package org.shj.demo;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.model.perceptron.PerceptronLexicalAnalyzer;
import com.hankcs.hanlp.seg.NShort.NShortSegment;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import org.shj.utils.StringUtils;

import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Shen Huang Jian
 * @date 2020-10-14 9:48
 */
public class PlaceRecognizeDemo {

    public static void main(String[] args){
        HanLP.Config.enableDebug();
        String[] texts = {"菜市场位于太平中桥西侧。",
//                "今天，刘志军案的关键人物,山西女商人丁书苗在市二中院出庭受审。",
//                "江西省监狱管理局与中国太平洋财产保险股份有限公司南昌中心支公司保险合同纠纷案",
//                "新北商贸有限公司"
        };

        for(String text : texts){
            method1(text);
//            method2(text);
//            method3(text);
            System.out.println();
        }

    }

    private static void method1(String text){
        Segment segment = HanLP.newSegment();
        segment.enablePlaceRecognize(true);

        Set<String> places = new HashSet<>();

        List<Term> termList = segment.seg(text);
        //System.out.println(termList);
        for(Term term : termList){
            if(Nature.ns.equals(term.nature)){
                places.add(term.word);
            }
        }

        System.out.println(StringUtils.join(places, ", "));
    }

    private static void method2(String text){
        Segment segment = new NShortSegment().enableCustomDictionary(true).enablePlaceRecognize(true).enableOrganizationRecognize(true);
        Set<String> places = new HashSet<>();



        List<Term> termList = segment.seg(text);
        //System.out.println(termList);
        for(Term term : termList){
            if(Nature.ns.equals(term.nature)){
                places.add(term.word);
            }
        }

        System.out.println(StringUtils.join(places, ", "));
    }

    private static void method3(String text){
        try {
            PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(HanLP.Config.PerceptronCWSModelPath,
                    HanLP.Config.PerceptronPOSModelPath,
                    HanLP.Config.PerceptronNERModelPath);

            /*String[] placeFiles = HanLP.Config.CustomDictionaryPath;
            for(String placeFile : placeFiles){
                if(placeFile.endsWith(" ns")){
                    String fileName = placeFile.substring(0, placeFile.lastIndexOf(" ns"));
                    RandomAccessFile rac = new RandomAccessFile(fileName, "r");
                    String line;
                    while((line = rac.readLine()) != null){
                        analyzer.learn(line + "/ns");
                    }
                }
            }*/

            Sentence sentence = analyzer.analyze(text);
            for(IWord word : sentence.wordList){

                if(word.getLabel().equals(Nature.ns.toString())){
                    System.out.println(word.getValue());
                }
            }

        }catch (Exception e){

        }
    }

}
